﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.SymptomHistory
{
    public class SurveyAdd : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected List<DiseaseSymptomInfo> PrimarySymptomList;
        /*protected List<DiseaseSymptomInfo> SecondSymptomList;*/

        protected new void HandleCommon()
        {
            this.AddLocation("症状记录", "/SymptomHistory/Index.aspx");
            this.AddLocation("批量更新", "/SymptomHistory/SurveyAdd.aspx");
            this.SetTitle("批量更新症状记录");

            PrimarySymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PrData.DiseaseId, Constants.PRIMARY_SYMPTOM);
            /* SecondSymptomList = DiseaseBLL.Instance.GetDiseaseSymptomList(PrData.DiseaseId, Constants.SECOND_SYMPTOM);*/

        }

        protected new void HandleGet()
        {
           
        }

        protected new void HandlePost()
        {
            List<SymptomHistoryInfo> lst = ValidateForm();
            if (lst == null) return;
            bool succ = true;

            PatientSymptomBLL.Instance.AddSymptomSurvey(lst);
            if (succ)
            {
                this.AddMsgLine("添加成功");
            }
            else 
            {
                this.AddErrLine("添加失败");
            }

            this.SetUrl("/SymptomHistory/Index.aspx");
            this.SetMetaRefresh();
        }

        protected List<SymptomHistoryInfo> ValidateForm()
        {
            // 对时间进行判断
            string value = RequestTools.GetFormString("date");
            if (value == "")
            {
                this.AddErrLine("请填写日期！");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的日期：" + value);
                return null;
            }

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            //else if (DateTime.Compare(date, PrData.StartDate) < 0)
            //{
            //    this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
            //    return null;
            //}

            //需要判断是否有相同时间的历史记录
            if (PatientBLL.Instance.CheckPatientSurvey(PrData.Id, Constants.SymptomSurvey, date))
            {
                //提示是否需要覆盖历史记录
                this.AddErrLine("是否需要覆盖" + PrData.RecordDate.ToString("yyyy-MM-dd") + "已有的批量历史记录？");
                return null;
            }

            List<SymptomHistoryInfo> lst = new List<SymptomHistoryInfo>();
            foreach (DiseaseSymptomInfo item in PrimarySymptomList)
            { 
                SymptomHistoryInfo info = new SymptomHistoryInfo();
                info.PRId = PrData.Id;
                info.SymptomId = item.SymptomId;
                info.DegreeValue = RequestTools.GetFormInt("sym" + item.SymptomId, -1);
                if (info.DegreeValue < 0)
                {
                    this.AddErrLine("请选择主要症状 （" + item.SymptomName + "） 对应的症状程度！");
                    return null;
                }

                info.RecordDate = date;
                info.UpdateDate = DateTime.Today;
                info.Type = Constants.PRIMARY_SYMPTOM;
                lst.Add(info);
            }
            return lst;
        }

    }
}